/**
 * 
 */
package org.huanglei.account.view.base;

import org.huanglei.account.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

/**
 * @author huanglei
 * @data Jul 16, 2012
 */
public abstract class TitleBarActivity extends Activity {

	/**
	 * 返回
	 */
	public static final ClickListener CL_BACK = new ClickListener() {

		@Override
		public void onClick(TitleBarActivity activity, View view) {
			activity.finish();
		}

		@Override
		public ClickListener setObject(Object object) {
			return null;
		}

	};

	/**
	 * 首页
	 */
	public static final ClickListener CL_HOME = new ClickListener() {

		private Class<?> clazz = null;

		@Override
		public void onClick(TitleBarActivity activity, View view) {
			if (clazz == null) {
				throw new NullPointerException("class must set");
			}
			Intent intent = new Intent();
			intent.setClass(activity, clazz);
			activity.startActivity(intent);
		}

		@Override
		public ClickListener setObject(Object object) {
			clazz = (Class<?>) object;
			return this;
		}
	};

	protected final Context me = this;

	/**
	 * titlebar 控件
	 * 
	 */
	public enum TitleBarView {
		// 最左边 新增
		TBV_NEW(R.id.titlebar_btn_new),
		// 最左边 返回
		TBV_BACK(R.id.titlebar_btn_back),
		// 中间
		TBV_TEXT(R.id.titlebar_title_text),
		// 最右边 按钮组容器
		TBV_WRAPPER(R.id.titlebar_btn_wrapper),
		// 最右边 按钮1
		TBV_WRAPPER_BTN1(R.id.titlebar_btn_wrapper_btn1),
		// 最右边 按钮2
		TBV_WRAPPER_BTN2(R.id.titlebar_btn_wrapper_btn2),
		// 最右边 首页
		TBV_HOME(R.id.titlebar_btn_home);

		private int id;

		private TitleBarView(int id) {
			this.id = id;
		}

		public int getId() {
			return id;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(getContentView());
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.titlebar);
		init();
	}

	/**
	 * 初始化
	 */
	protected abstract void init();

	/**
	 * 获取layout Id
	 * 
	 * @return
	 */
	protected abstract int getContentView();

	/**
	 * 获取标题控件
	 * 
	 * @param tbv
	 * @return
	 */
	protected View getTitleBarView(TitleBarView tbv) {
		return findViewById(tbv.getId());
	}

	/**
	 * 设置标题文字
	 * 
	 * @param title
	 */
	public void setBarTitle(String title) {
		TextView textView = (TextView) getTitleBarView(TitleBarView.TBV_TEXT);
		textView.setText(title);
	}

	/**
	 * 显示标题控件
	 * 
	 * @param tbv
	 * @return
	 */
	public View showBarView(TitleBarView tbv) {
		return showBarView(tbv, null);
	}

	/**
	 * 显示标题控件
	 * 
	 * @param tbv
	 * @param clickListener
	 * @return
	 */
	public View showBarView(TitleBarView tbv, final ClickListener clickListener) {
		View view = getTitleBarView(tbv);
		view.setVisibility(View.VISIBLE);
		if (clickListener != null) {
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					clickListener.onClick((TitleBarActivity) me, v);
				}
			});
		}
		return view;
	}

	/**
	 * 标题点击事件
	 */
	public interface ClickListener {

		public void onClick(TitleBarActivity activity, View view);

		public ClickListener setObject(Object object);
	}

}
